import { LatLng } from "../LatLng.js";
import { Point } from "../../geometry/Point.js";
const earthRadius = 6378137;
export default class SphericalMercator {
  R = earthRadius;
  constructor() {
  }
  // 经纬度转web墨卡托（4326转3857）
  project(latlng) {
    const d = Math.PI / 180,
    sin = Math.sin(latlng.lat * d);
    return new Point(
      this.R * latlng.lng * d,
      (this.R * Math.log((1 + sin) / (1 - sin))) / 2
    );
  }
  // web墨卡托转经纬度（3857转4326）
  unproject(point) {
    const d = 180 / Math.PI;
    return new LatLng(
      (2 * Math.atan(Math.exp(point.y / this.R)) - Math.PI / 2) * d,
      (point.x * d) / this.R
    );
  }
}
export const sphericalMercator = () => {
  return new SphericalMercator();
};
